package com.logic.landseaserver.task;

import com.logic.landseaserver.service.IScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.service.IExpensesService;
import com.logic.system.service.IConfigurationService;
import com.logic.system.service.impl.ConfigurationService;

import java.util.Calendar;
import java.util.Date;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：ExpensesMonthTask</br>
 * 类描述：根据每月费用(导入)生成账单</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月26日 下午2:31:27</br>
 * @version 1.0
 *
 */
@Component
public class ExpensesMonthTask {

    private static final Logger log = LoggerFactory.getLogger(ExpensesMonthTask.class);

    @Autowired
    private IConfigurationService configurationService;

    @Autowired
    private IExpensesService expensesServiceImpl;

    @Autowired
    private IScheduleService iScheduleService;
    /**
     * 
     * [简要描述]：定时任务每天凌晨1点启动</br>
     * [详细描述]：按照指定日期判断是否执行</br>
     * [作者]：Aaron(2017-07-26)</br>
     *
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void generateBill() 
    {
        int excuteDay =Integer.parseInt(configurationService.getStringValue(ConfigurationService.EXPENSES_CREATE_MONTHLY_DAY));

        // 如果不是指定的执行日期，则退出
        if (!DateUtil.checkIsDayOfMonth(excuteDay))
        {
            log.info("today is not " + excuteDay + ",generate expenses bill task can not run.");
            return;
        }
        log.info("Task for ExpensesMonthTask start.");
        // 开始执行业务
        try {
            boolean cuteFlag = iScheduleService.cuteSchedule();
            if(cuteFlag){
                expensesServiceImpl.batchInertMonthly();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("createMonthFormEx Exception" + e.getMessage());
        }
        log.info("Task for ExpensesMonthTask end.");
    }


    /**
     *
     * [简要描述]：定时任务每天凌晨1点启动</br>
     * [详细描述]：按照指定日期判断是否执行</br>
     * [作者]：Aaron(2017-07-26)</br>
     *
     */
    @Scheduled(cron = "0 0 1 6,26 * ?")
    public void batchInsertDingxinDate()
    {
        log.info("批量汇总鼎信度数 start");
        try {
            boolean cuteFlag = iScheduleService.cuteSchedule();
            if (cuteFlag) {
                String date = DateUtil.getInstence().convertDate(DateUtil.getInstence().getAppointDay(Calendar.DAY_OF_MONTH, -3));
                expensesServiceImpl.batchInsertDingxinDate(date);
            }
        } catch (Exception e) {
            log.info("批量汇总鼎信度数 error:" + e);
            e.printStackTrace();
        }
        log.info("批量汇总鼎信度数 end");
    }
}
